package com.gt.gesture;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent;
import android.widget.Toast;

public class GestureTestActivity extends Activity implements OnGestureListener {
	/** Called when the activity is first created. */
	private GestureDetector gestureScanner;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		gestureScanner = new GestureDetector(this);
	}

	@Override
	public boolean onTouchEvent(MotionEvent me) {
		return gestureScanner.onTouchEvent(me);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		return true;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

		System.out.println("1 \t" + e1.getX() + "," + e1.getY());
		System.out.println("2 \t" + e2.getX() + "," + e2.getY());
		System.out.println("VelocityX :" + velocityX);
		System.out.println("VelocityY :" + velocityY);
		float dispX = e2.getX() - e1.getX();
		float dispY = e2.getY() - e1.getY();
		if (Math.abs(dispX) >= 200 && Math.abs(dispY) <= 50) {
			// swipe ok
			if (dispX > 0) {
				// L-R swipe
				System.out.println("L-R swipe");
			} else {
				// R-L swipe
				System.out.println("R-L swipe");
			}
		}
		return true;
	}

	@Override
	public void onLongPress(MotionEvent e) {
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return true;
	}

	@Override
	public void onShowPress(MotionEvent e) {
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return true;
	}

	private void showToast(String msg) {
		System.out.println(msg);
		Toast toast;
		toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
		toast.show();
	}
}